<?php
/*Problem Domain
To perform the actual CRUD operations on Technology by implementing the TechnologyDAO interface
*/
/*
include_once 'TechnologyDAO.php';
include_once '../../model/Technology.php';
include_once '../../model/ListUserType.php';
*/
class TechnologyDAOImpl implements TechnologyDAO
{
	private $conn;
	private $array_list;

	/*void 
	purpose. initializes the newly created TechnologyDAOImpl.*/
	public function __construct()
	{
		$this->conn = new mysqli('localhost' , 'root' , 'root123' , 'tms') or
		die('Connect error : '.$this->conn->connect_error);

		$this->array_list = new ListUserType();
	}

	/*void -> Connection
	purpose. produces the connection object*/
	public function getConnection()
	{
		return $this->conn;
	}

	/*Technology -> boolean
	purpose. produces true if the given Technology is found , false otherwise*/
	private function found($Technology)
	{
		$conn = $this->getConnection();
		$Technology_id = $Technology->getId();
		$res = $conn->query("select * from Technology where tech_id = ".$Technology_id);
		$status;
		
		if($res->num_rows == 0)
		{
			$status = false;
		}
		else
		{
			$status = true;
		}

		return $status;
	}

	/*Technology -> boolean
	purpose. To persist the given Technology into the database and return true on success
	false otherwise*/
	private function save($Technology)
	{
		$conn 	   = $this->getConnection();
		$tech_name = $Technology->getTechName();
		$desc	   = $Technology->getDesc();

		$stmt = $conn->prepare('Insert into Technology values (null , ? , ?)');
		$status = true;

		if(!$stmt)
		{
			echo "\n Prepared Statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('ss', $tech_name, $desc))
			{
				echo "Bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
				else
				{
					$id = $conn->insert_id;
					$Technology->setId($id);
				}
			}
		}

		return $status;		
	}

	/*Technology -> boolean
	pupose. To update the already existing given Technology and return true on success , false otherwise.*/
	private function update($Technology)
	{
		$conn       = $this->getConnection();
		$Technology_id = $Technology->getId();
		$tech_name  = $Technology->getTechName();
		$desc	    = $Technology->getDesc();
		$status = true;

		$stmt = $conn->prepare('update Technology set tech_title = ? , `desc` = ? where tech_id = ?');

		if(!$stmt)
		{
			echo "Prepare statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('ssi', $tech_name, $desc, $Technology_id))
			{
				echo "Parameter bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
			}
		}

		return $status;
	}

	/*Technology -> boolean
        purpose. to save or update a given Technology into the database and return true on success , 
        false otherwise*/
        public function saveOrUpdate($Technology)
	{
		$status;

		if(!$this->found($Technology))
		{
			$status = $this->save($Technology);			
		}
		else
		{
			$status = $this->update($Technology);
		}

		return $status;
	}	

	/*Technology -> boolean
        purpose. removes a given Technology from the database and returns true
        on success , false otherwise*/
        public function removeTech($tech_id)
	{
		$conn = $this->getConnection();
		$status = true;

		$sql     = "delete from `Technology` where `tech_id` = ".$tech_id;
		echo "$sql";

		if(!$conn->query($sql))
		{
			echo "Query Execution error no : ".$conn->errno."error : ".$conn->error;
			$status = false;
		}
		

		return $status;
	}

	/*int -> ptring
        purpose. produces the Technology name for the give Technology ID*/
        public function getNameFromId($id)
	{
		$conn = $this->getConnection();
		$sql = "select `tech_title` from Technology where `tech_id` = ".$id;
		$res = $conn->query($sql);
		$tech_title = null;
		
		if(!$res)
                {
                        echo "Query Execution error no : ".$conn->errno."error : ".$conn->error;
                }
                else
                {
                        $row = $res->fetch_assoc();
                        $tech_title = $row['tech_title'];
                }

                return $tech_title;
	}

	
	/*void -> LostUserType
	purpose. produces the entire list of Technologies of the system*/
	public function listTechnology()
	{
		$conn = $this->getConnection();
		$sql = 'select * from Technology';
		$res = $conn->query($sql);

		while($row = $res->fetch_assoc())
		{
			$Technology_id 	= $row['tech_id'];
			$tech_name 	= $row['tech_title'];
			$desc		= $row['desc'];
			
			$Technology	= new Technology($tech_name, $desc, $Technology_id);

			$this->array_list->addToList($Technology);
		}

		return $this->array_list;
	}

	public function getIdFromName($tech_name)
	{
		$conn = $this->getConnection();
		$sql  = "SELECT `tech_id` FROM `Technology` WHERE `tech_title` = '".$tech_name."'";

		$res  = $conn->query($sql);

		if(!$res)
                {
                        echo "Query Execution error no : ".$conn->errno."error : ".$conn->error;
                }
		else
		{
			$row  = $res->fetch_assoc();
			$tech_id = $row['tech_id']; 
		}

		return $tech_id;
	}
}
 
?>
